// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT

use clap::{Parser, ValueEnum};
use jpg_aigc_meta::{error_discriminant, read_aigc_meta, save_aigc_meta};
use std::path::PathBuf;

#[derive(Parser, Debug)]
struct Args {
    #[arg(short = 't', long = "type", required = true)]
    operation: Operation,
    #[arg(short = 's', long = "src", required = true)]
    src_path: String,
    #[arg(short = 'd', long = "dest")]
    dest_path: Option<String>,
    #[arg(short = 'l', long = "label")]
    label: Option<String>,
    #[arg(short = 'p', long = "provider-id")]
    provider_id: Option<String>,
    #[arg(short = 'c', long = "content-provider")]
    content_provider: Option<String>,
    #[arg(short = 'a', long = "as-producer")]
    as_producer: Option<bool>,
}

#[derive(Debug, Clone, ValueEnum)]
enum Operation {
    #[clap(name = "read")]
    Read,
    #[clap(name = "write")]
    Write,
}

fn main() -> Result<(), String> {
    let args = Args::parse();

    if matches!(args.operation, Operation::Write) {
        if args.dest_path.is_none() {
            return Err(format!("dest_path not found"));
        }
        if args.label.is_none() {
            return Err(format!("label not found"));
        }
        if args.provider_id.is_none() {
            return Err(format!("provider_id not found"));
        }
        if args.content_provider.is_none() {
            return Err(format!("content_provider not found"));
        }
        if args.as_producer.is_none() {
            return Err(format!("as_producer not found"));
        }
    }

    match args.operation {
        Operation::Read => {
            let path = PathBuf::from(args.src_path);
            let result = read_aigc_meta(&path);
            match result {
                Ok(label) => {
                    println!("read_aigc_meta success. aigc_meta:{}", label);
                }
                Err(e) => {
                    println!(
                        "read_aigc_meta failed. err_code:{}, err_desc:{:?}",
                        error_discriminant(&e),
                        e
                    );
                }
            }
        }
        Operation::Write => {
            let src_path = PathBuf::from(args.src_path);
            let dest_path = PathBuf::from(args.dest_path.unwrap());

            let result = save_aigc_meta(
                &src_path,
                &dest_path,
                args.label.unwrap(),
                args.content_provider.unwrap(),
                args.provider_id.unwrap(),
                args.as_producer.unwrap(),
            );
            match result {
                Ok(label) => {
                    println!("save_aigc_meta success. aigc_meta:{}", label);
                }
                Err(e) => {
                    println!(
                        "save_aigc_meta failed. err_code:{}, err_desc:{:?}",
                        error_discriminant(&e),
                        e
                    );
                }
            }
        }
    }
    Ok(())
}
